//
//  ContainerView.m
//  RunLoopDemo
//
//  Created by zhangjikuan on 2020/11/25.
//  Copyright © 2020 hsgd. All rights reserved.
//

#import "ContainerView.h"

#import "YellowView.h"
#import "BlackView.h"
#import "BlueView.h"
#import "WhiteView.h"
#import "GreenView.h"

#define kInterval 20

@implementation ContainerView
{
    ViewBase *_yellowView;
    ViewBase *_blackView;
    ViewBase *_blueView;
    ViewBase *_whiteView;
    ViewBase *_greenView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _addSubViews];
    }
    return self;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self _addSubViews];
    }
    return self;
}

#pragma mark - Private

- (void)_addSubViews {
    
    _yellowView = [self _view:[YellowView class] withFrame:[self _frameWithSortIndex:0]];
    _blackView = [self _view:[BlackView class] withFrame:[self _frameWithSortIndex:1]];
    _blueView = [self _view:[BlueView class] withFrame:[self _frameWithSortIndex:2]];
    _whiteView = [self _view:[WhiteView class] withFrame:[self _frameWithSortIndex:3]];
    _greenView = [self _view:[GreenView class] withFrame:[self _frameWithSortIndex:4]];
    
    [self addSubview:_yellowView];
    [self addSubview:_blackView];
    [self addSubview:_blueView];
    [self addSubview:_whiteView];
    [self addSubview:_greenView];
    
    [self addSubview: [self _view:[WhiteView class] withFrame:[self _frameWithSortIndex:5]]];
    [self addSubview: [self _view:[BlueView class] withFrame:[self _frameWithSortIndex:6]]];
    [self addSubview: [self _view:[BlackView class] withFrame:[self _frameWithSortIndex:7]]];
    [self addSubview: [self _view:[YellowView class] withFrame:[self _frameWithSortIndex:8]]];
    [self addSubview: [self _view:[BlackView class] withFrame:[self _frameWithSortIndex:9]]];
}

- (CGRect)_frameWithSortIndex:(NSInteger)index {
    CGFloat x = index * 20 ;
    CGFloat y = index * 20 + 80;
    CGFloat width = self.bounds.size.width - 2 * x;
    CGFloat height = self.bounds.size.height - 2 * y;
    return CGRectMake(x, y, width, height);
}

- (ViewBase *)_view:(Class)viewClass withFrame:(CGRect)cFrame {
    ViewBase *view = [[viewClass alloc] init];
    view.frame = cFrame;
    return view;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(_yellowView.frame, point)) {
        CGPoint points = [_greenView convertPoint:point fromView:self];
        BOOL clickSelf = [_greenView pointInside:points withEvent:event];
        
        // 兄弟视图的相互转换
        if (clickSelf) {
            [_yellowView printDescription];
            return _yellowView;
        }
        // 兄弟视图响应
        return _greenView;
    }

    if (CGRectContainsPoint(_blackView.frame, point)) {
        [_blackView printDescription];
        
        return _yellowView;
    }
    if (CGRectContainsPoint(_blueView.frame, point)) {
        [_blueView printDescription];
    }
    if (CGRectContainsPoint(_whiteView.frame, point)) {
        [_whiteView printDescription];
    }
    if (CGRectContainsPoint(_greenView.frame, point)) {
        [_greenView printDescription];
    }
    
    
    return self;
}

@end
